package co.fitstart.api.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.demo2do.core.persistence.GenericDaoSupport;

import co.fitstart.entity.coupon.Coupon;
import co.fitstart.entity.coupon.stat.CouponTemplateStat;

/**
 * 
 * @author mohanlan
 *
 */
@Repository("couponDaoSupport")
public class CouponDaoSupport {
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;
    
    /**
     * 
     * @param coupon
     * @param amount product current price
     */
    public void userCoupon(Coupon coupon, int amount) {
        if(coupon.getTemplate().getDiscountType().isFixedAmount()) {
            amount = coupon.getAmount();
        } else {
            coupon.setAmount(amount);
        }
        genericDaoSupport.update(coupon.use());
        CouponTemplateStat couponTemplateStat = genericDaoSupport.searchForObject("FROM CouponTemplateStat couponTemplateStat WHERE couponTemplateStat.couponTemplate = :couponTemplate", "couponTemplate", coupon.getTemplate(), CouponTemplateStat.class);
        genericDaoSupport.update(couponTemplateStat.useCoupon(1, amount));
    }
}
